import com.webservice.dto.Table;
import com.webservice.service.RmGetDataApiService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.StringReader;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


public class XmlTest {
    @Test
    public void test() throws IOException, DocumentException {
//        LocalDateTime localDateTimeStart = LocalDateTime.now();
//        System.out.println("开始时间:"+ localDateTimeStart);
//        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
//        String docType = "CUX_PO_VENDOR_Q";
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("STARTTIME","201804270002");
//        map.put("ENDTIME","201805270002");
//        map.put("ORG_ID","81");
//        String xmlStr = rmGetDataApiService.getDate(docType, map);
//
//        Document document = DocumentHelper.parseText(xmlStr);
//        Element root = document.getRootElement();
//        System.out.println("root:" + root.getName());
//        Element node=root.element("X_RESPONSE_DATA");
//        System.out.println(node.getStringValue());
//        xmlStr = node.getStringValue();
//        xmlStr = xmlStr.replace("<head>","");
//        xmlStr = xmlStr.replace("</head>","");
//        xmlStr = xmlStr.replace("&","&amp;");//非法字符
//        try {
//            JAXBContext context = JAXBContext.newInstance(Table.class);
//            Unmarshaller unmarshaller = context.createUnmarshaller();
//            Table table = (Table)unmarshaller.unmarshal(new StringReader(xmlStr));
//            System.out.println(table.getRoot());
//            if(table.getRoot() != null){
//                for (int i = 0 ; i < table.getRoot().size() ; i++){
//                    System.out.println(table.getRoot().get(i).toString());//获取到对应的object
//                }
//            }
//        } catch (JAXBException e) {
//            e.printStackTrace();
//        }
//        LocalDateTime localDateTimeEnd = LocalDateTime.now();
//        System.out.println("结束时间:"+ localDateTimeEnd);
//        Duration duration = Duration.between(localDateTimeStart, localDateTimeEnd);
//        System.out.println("耗时: "+duration.toMillis());
    }

    @Test
    public void test1() throws IOException, DocumentException {
//        LocalDateTime localDateTimeStart = LocalDateTime.now();
//        System.out.println("开始时间:"+ localDateTimeStart);
//        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
//        String docType = "CUX_INV_DEPT_Q";
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("START_DATE","201804270002");
//        map.put("END_DATE","202005270002");
//        map.put("ORG_ID","102");
////        map.put("DEPT_ID","10");
//        String xmlStr = rmGetDataApiService.getDate(docType, map);
//
//        Document document = DocumentHelper.parseText(xmlStr);
//        Element root = document.getRootElement();
//        Element return_code = root.element("X_RETURN_CODE");
//        Element return_mesg = root.element("X_RETURN_MESG");
//        if (return_code.getStringValue().equals("S1001000")){
//            System.out.println("状态码: S1001000 发送接收成功");
//        }else{
//            System.out.println("状态码: " + return_code.getStringValue() + " 发送接收失败\n返回信息:" + return_mesg.getStringValue());
//        }
//        Element node=root.element("X_RESPONSE_DATA");
//        System.out.println(node.getStringValue());
//        xmlStr = node.getStringValue();
//        xmlStr = xmlStr.replace("<head>","");
//        xmlStr = xmlStr.replace("</head>","");
//        xmlStr = xmlStr.replace("&","&amp;");//非法字符
//        try {
//            JAXBContext context = JAXBContext.newInstance(Table.class);
//            Unmarshaller unmarshaller = context.createUnmarshaller();
//            Table table = (Table)unmarshaller.unmarshal(new StringReader(xmlStr));
////            System.out.println(table.getRoot());
//            if(table.getRoot() != null){
//                for (int i = 0 ; i < table.getRoot().size() ; i++){
//                    System.out.println(table.getRoot().get(i).deptToString());//获取到对应的object
//                }
//            }
//        } catch (JAXBException e) {
//            e.printStackTrace();
//        }
//        LocalDateTime localDateTimeEnd = LocalDateTime.now();
//        System.out.println("结束时间:"+ localDateTimeEnd);
//        Duration duration = Duration.between(localDateTimeStart, localDateTimeEnd);
//        System.out.println("耗时: "+duration.toMillis());
    }
}
